package com.gnote.pojo;

import org.springframework.ai.chat.client.ChatClientRequest;
import org.springframework.ai.chat.client.ChatClientResponse;
import org.springframework.ai.chat.client.advisor.api.*;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.chat.prompt.PromptTemplate;

import java.util.Map;

public class ReReadingAdvisor implements BaseAdvisor {
    private final static String RE_READING_PROMPT = """            
            {re2_input_query}
            read the question again :{re2_input_query}
            """;

    @Override
    public ChatClientRequest before(ChatClientRequest chatClientRequest, AdvisorChain advisorChain) {
        // 用户提示词
        String contents = chatClientRequest.prompt().getContents();
        String re2InputQuery = PromptTemplate.builder().template(RE_READING_PROMPT).build()
                .render(Map.of("re2_input_query", contents));
        //复制chatclient
        ChatClientRequest reChatClientReq = chatClientRequest.mutate().prompt(Prompt.builder().content(re2InputQuery).build()).build();

        return reChatClientReq;
    }

    @Override
    public ChatClientResponse after(ChatClientResponse chatClientResponse, AdvisorChain advisorChain) {
        //在请求之后处理响应
        return chatClientResponse;
    }

    @Override
    public int getOrder() {
        return 0;
    }

}
